package com.free.semantic.core.impl.demo;

import com.free.semantic.common.LocatorNode;
import com.free.semantic.common.Ls;
import com.free.semantic.common.ParamNode;
import com.free.semantic.common.RelationChain;
import com.free.semantic.core.ConceptInstanceManage;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.impl.BaseTest;
import com.free.semantic.core.impl.ConceptInstanceManageImpl;
import com.free.semantic.req.RelationDefineGroup;
import com.free.semantic.utils.Utils;
import org.junit.jupiter.api.Test;

import java.util.List;

import static com.free.semantic.common.RangeObj.one;

/**
 * 语义网络demo演示，语义分支
 */
public class Demo3_semanticBranch extends BaseTest {


    @Test
    public void test() {

        RelationDefineGroup group = new RelationDefineGroup();
        /**
         *  relation 单属性(A,B)
         *    A->B=1 //代表 针对任意一个A 都可以找到 1个B与之对应
         */
        defineRelation(group, "属性", defineItem(oneObj("A"), oneObj("B")));

        //  使用包含关系 定义一个语义网络
        ConceptManage personInfoModel = getConceptManage(group);
        //object 人 身高 体重
        personInfoModel.addConcept("人", "身高", "体重", "数字");

        //属性(人,身高)
        createRelation(personInfoModel, "属性", "人", "身高");
        //属性(人,体重)
        createRelation(personInfoModel, "属性", "人", "体重");
        //单属性(身高,数字)
        createRelation(personInfoModel, "属性", "身高", "数字");
        //单属性(体重,数字)
        createRelation(personInfoModel, "属性", "体重", "数字");

        List<RelationChain> relationChains1 = personInfoModel.findAllRelationChain(Ls.of("人"), "数字");

        assert relationChains1.size() == 2;

        List<RelationChain> relationChains2 = personInfoModel.findAllRelationChain(Ls.of("身高"), "数字");
        assert relationChains2.size() == 1;

        ConceptInstanceManage modelInstance = new ConceptInstanceManageImpl(personInfoModel,
                buildMap("人", "体重", new String[][]{{"张三", "65kg"}}),
                buildMap("人", "身高", new String[][]{{"张三", "180cm"}}),
                Utils.buildMap("体重", "数字", new String[][]{{"65kg", "65"}}),
                Utils.buildMap("身高", "数字", new String[][]{{"180cm", "180"}})
        );
        List<Object> objects2 = modelInstance.goByLocatorNode(Ls.of(new ParamNode("人", "张三"), new LocatorNode("体重")), one("数字"));
        assert objects2.equals(Ls.of("65"));
    }

}
